package 基础篇.线程同步.同步监视器;

/**
 * @author IBM CHEN ZHI KAN
 * @create 2018/7/28
 */
public class DrawThread extends Thread{
  private Account account;
  private double drawAmount;
  public DrawThread(Account account,double drawAmount){
    super();
    this.account=account;
    this.drawAmount=drawAmount;
  }
  public void run() {
    //对高并发访问的共享资源充当同步监视器
    synchronized (account) {
      if (account.getBalance() >= drawAmount) {
        System.out.println("取钱成功，吐出钞票");
        try {
          Thread.sleep(1000l);
        } catch (Exception e) {
          e.printStackTrace();
        }
        account.setBalance(account.getBalance() - drawAmount);
        System.out.println("余额为：" + account.getBalance());
      } else {
        System.out.println(getName() + "余额已取完");
      }
    }
  }
}
